package com.example.smallandroidcodetester;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import net.reea.example.multiselect.MultiSelectionActivity;
import net.reea.example.viewpager.FragmentPagerSupport;
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.FaceDetector;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Parent extends Activity implements SurfaceHolder.Callback {

	private ArrayList<Example> examples = new ArrayList<Example>();
	private LayoutInflater inflater;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.parent);

		ListView exampleList = (ListView) findViewById(R.id.ExampleList);
		exampleList.setAdapter(exampleAdapter);
		exampleList.setOnItemClickListener(onExampleClickListener);
		populateExamples(FaceDetection.class, "Face Detection");
		populateExamples(EventBusExample.class, "Event Bus");
		populateExamples(FaceDetection2.class, "FaceDetection2");
		populateExamples(ListMultiChoice.class, "ListMultiChoice");
		populateExamples(MultiSelectionActivity.class, "LibraryActivity");
		populateExamples(FragmentPagerSupport.class, "FragmentPagerSupport");
		// TextView tv = (TextView) findViewById(R.id.ProgressText);
		// tv.setText("Ana Lombard -Global Therapist Body- Mind - Emotions\n\nAna Lombard first started her education with relaxation and meditation techniques. Moreover, she chose to specialize in managing stress and emotions, and became a sophrologist and then completed a Master\'s degree in craniosacral osteopathy. She defined her techniques in what she termed a BMAM (Body &amp; Mind Alternative Method) because according to Ana a well-managed mind helps heal the body and the regenerated body allows us to develop our skills and capabilities.\n\nIn May 2008, Ana created the Enlace Center of conventional and alternative medicine in Barcelona. (<u>www.enlacebcn.com</u>) and an App to manage the stress : iDStress ( <u>www.idstress.com</u>)\n\nQuestions and informations at : <u>info@enlacebcn.com</u>");
		//
		// Linkify.addLinks(tv, Linkify.ALL);
		//
		// MediaPlayer mp = new MediaPlayer();
		// try {
		// mp.setDataSource(this, Uri.parse("file://android_asset/untitled.mp3"));
		// mp.prepare();
		// } catch (IllegalArgumentException e1) {
		// e1.printStackTrace();
		// } catch (SecurityException e1) {
		// e1.printStackTrace();
		// } catch (IllegalStateException e1) {
		// e1.printStackTrace();
		// } catch (IOException e1) {
		// e1.printStackTrace();
		// }
		//
		// mp.setOnPreparedListener(new OnPreparedListener() {
		//
		// @Override
		// public void onPrepared(MediaPlayer mp) {
		// mp.start();
		// }
		// });
		//
		// try {
		// for (String s : getAssets().list("")) {
		// Log.i("locsis : ", "locsics : " + s);
		// Log.i("locsis : ", "locsics2 : " + getAssets().toString());
		// }
		// } catch (IOException e) {
		// e.printStackTrace();
		// }

		// List<PackageInfo> packages = getPackageManager().getInstalledPackages(PackageManager.GET_ACTIVITIES);
		// PackageInfo eBibliaPackageInfo = null;
		// for (PackageInfo packageInfo : packages) {
		// // if (packageInfo.packageName.equalsIgnoreCase("net.quickbible")) {
		// Log.i("", "==== application info : " + packageInfo);
		// // eBibliaPackageInfo = packageInfo;
		// // Intent launchIntent = pm.getLaunchIntentForPackage(packageName);
		// // getApplicationContext().startActivity(launchIntent);
		// // }
		// }

		// execCommand2();

		// execCommand3();

		// WebView wv = (WebView) findViewById(R.id.webview);
		// wv.loadUrl("file:///android_asset/test_animation.gif");
		// wv.setBackgroundColor(0x00000000);

		// Crashlytics.start(this);
		// setContentView(R.layout.activity_main);
		//
		// final VideoView vv = (VideoView) findViewById(R.id.vv);
		// final CustomProgressDialog dialog = new CustomProgressDialog(this);
		//
		// cdt.start(1000);
		// // String url =
		// // "https://panda-assets-prod.s3.amazonaws.com/uploads/document/video/2721/9ab7fe600c480e01527791b1619d43b1.mp4";
		// String url = "http://www.reea.net/clients/B/assetpanda/a.mp4";
		// MediaController mediaController = new MediaController(this);
		// mediaController.setMediaPlayer(vv);
		// mediaController.setAnchorView(vv);
		// vv.setMediaController(mediaController);
		// vv.setVideoURI(Uri.parse(url));
		//
		// SurfaceHolder holder = vv.getHolder();
		// holder.addCallback(this);
		//
		// vv.setOnErrorListener(new OnErrorListener() {
		//
		// @Override
		// public boolean onError(MediaPlayer mp, int what, int extra) {
		// Toast.makeText(CustomTabActivity.this, "ERROR", Toast.LENGTH_SHORT).show();
		// return false;
		// }
		// });
		//
		// vv.setOnPreparedListener(new OnPreparedListener() {
		//
		// @Override
		// public void onPrepared(MediaPlayer mp) {
		// Toast.makeText(CustomTabActivity.this, "PREPARED", Toast.LENGTH_SHORT).show();
		// dialog.hide();
		// }
		// });
		//
		// Button b = (Button) findViewById(R.id.startVideo);
		// b.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// vv.start();
		// dialog.show();
		// }
		// });

	}

	private void setupNewItem() {

	}

	private void execCommand2() {
		try {
			File file = Environment.getExternalStorageDirectory();
			String path = file.getPath();
			Process process = Runtime.getRuntime().exec(new String[] { "su", "-c", "mkdir", "zsolt" }, new String[] { path }, null);
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

			StringBuilder log = new StringBuilder();
			String line;
			while ((line = bufferedReader.readLine()) != null) {
				log.append(line + "\n");
			}
			TextView tv = (TextView) findViewById(R.id.Button2);
			tv.setText(log.toString());
		} catch (IOException e) {
			Log.e("CCC", e.getMessage());
		}
	}

	private void execCommand3() {
		try {
			Process process = Runtime.getRuntime().exec(new String[] { "ls" });
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

			StringBuilder log = new StringBuilder();
			String line;
			while ((line = bufferedReader.readLine()) != null) {
				log.append(line + "\n");
			}
			TextView tv = (TextView) findViewById(R.id.Button2);
			tv.setText(log.toString());
		} catch (IOException e) {
			Log.e("CCC", e.getMessage());
		}
	}

	private void execCommand() {
		try {
			// Process p = Runtime.getRuntime().exec("test");
			Process process = Runtime.getRuntime().exec(new String[] { "ls" }, null, null);
			DataOutputStream pOut = new DataOutputStream(process.getOutputStream());
			pOut.writeBytes("ls");
			pOut.flush();
			pOut.writeBytes("exit\n");
			pOut.flush();
			try {
				process.waitFor();
			} catch (InterruptedException e) {
				DialogFactory.showInfo(Parent.this, "not root");
			}
			if (process.exitValue() == 0)
				DialogFactory.showInfo(Parent.this, "root");
			else
				DialogFactory.showInfo(Parent.this, "not root");
		} catch (IOException e) {
			Log.e("CCC", e.getMessage());
		}
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		System.out.println("CustomTabActivity.surfaceChanged()");
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		System.out.println("CustomTabActivity.surfaceCreated()");
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		System.out.println("CustomTabActivity.surfaceDestroyed()");
	}

	private void faceDetection() {
		// ImageView family = (ImageView) findViewById(R.id.FamilyImage);
		FaceDetector faceDetector = new FaceDetector(851, 564, 1);
		FaceDetector.Face[] faces = new FaceDetector.Face[1];
		AssetManager am = getAssets();
		InputStream istr;
		Bitmap bitmap = null;
		try {
			istr = am.open("family.jpg");
			bitmap = BitmapFactory.decodeStream(istr);
			// family.setImageBitmap(bitmap);
			int faceCount = faceDetector.findFaces(bitmap, faces);
			System.out.println("faceCount : " + faceCount);
			System.out.println("facesLength : " + faces.length);
		} catch (IOException e) {
			System.out.println("ERROR : " + e);
		}

	}

	public class Example {

		private Class className;
		private String exampleTitle;

		public Example(Class className, String exampleTitle) {
			this.className = className;
			this.exampleTitle = exampleTitle;
		}

		public Class getClassName() {
			return className;
		}

		public void setClassName(Class className) {
			this.className = className;
		}

		public String getExampleTitle() {
			return exampleTitle;
		}

		public void setExampleTitle(String exampleTitle) {
			this.exampleTitle = exampleTitle;
		}

	}

	private BaseAdapter exampleAdapter = new BaseAdapter() {

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			inflater = (LayoutInflater) LayoutInflater.from(parent.getContext());
			View v = inflater.inflate(R.layout.example_list_item, null);
			TextView exampleTitle = (TextView) v.findViewById(R.id.ExampleTitle);
			exampleTitle.setText(examples.get(position).getExampleTitle());
			return v;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public Object getItem(int position) {
			return examples.get(position);
		}

		@Override
		public int getCount() {
			return examples.size();
		}
	};

	private void populateExamples(Class className, String exampleTitle) {
		examples.add(new Example(className, exampleTitle));
	}

	private OnItemClickListener onExampleClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			startActivity(new Intent(Parent.this, examples.get(position).getClassName()));
		}
	};
}
